package com.egao.texasHoldEmPoker.biz.impl;

import com.egao.common.core.damon.constant.UserConstant;
import com.egao.common.core.damon.results.BaseDao;
import com.egao.common.core.damon.results.PageResult2;
import com.egao.common.core.damon.utils.Utils;
import com.egao.common.system.entity.User;
import com.egao.texasHoldEmPoker.biz.DeskBiz;
import com.egao.texasHoldEmPoker.entity.Desk;
import com.egao.common.core.web.JsonResult;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.Map;
import java.util.List;

/**
 *
 * 数据层实现类
 * @author : Doman
 * @date : 2022-01-26 12:39:26
 * @version ：1.0
 */
@Repository
public class DeskBizImpl extends BaseDao implements DeskBiz {

	@Override
	public JsonResult list(PageResult2<T> page, Desk desk, User loginUser) throws Exception {
		Map<String, String> map  = new HashMap<String, String>();
		RowMapper<Desk> rm = new BeanPropertyRowMapper<Desk>(Desk.class);
		StringBuffer sql = new StringBuffer("select d.id,d.organization_id,d.name");
    sql.append(",d.increase_position,d.state,d.poker1,d.poker2,d.poker3");
		sql.append(",d.poker4,d.poker5,d.remark,d.order_num");
		sql.append(",d.blind,d.next_position,d.change_jeton");
		sql.append(",d.create_time,d.create_user,d.update_time,d.update_user");
    sql.append(",sogzn.organization_name organizationName,suser.username createName ");
    sql.append(" from th_desk d ");
    sql.append(" inner join sys_user suser on suser.user_id=d.create_user ");
    sql.append(" inner join sys_organization sogzn on sogzn.organization_id=d.organization_id and sogzn.deleted=0 ");
    sql.append(" where d.deleted=0 ");
		if(loginUser.getUserId() != UserConstant.ADMIN_ID){
      sql.append(" and d.organization_id=:tableOrganizationId ");
      map.put("tableOrganizationId", loginUser.getOrganizationId() + "");
    }
    if(Utils.isNotEmpty(desk)){
//			if(Utils.isNotEmpty(desk.getName())){
//				sql.append(" and instr(d.name,:name)>0 ");
//				map.put("name", desk.getName());
//			}
    if(Utils.isNotEmpty(desk.getSort())&&Utils.isNotEmpty(desk.getOrder())){
        sql.append(" order by d."+desk.getSort()+" "+desk.getOrder());
      } else {
        sql.append(" order by d.create_time desc");
      }
		}
		return jsonResult(getPageQueryList(page, sql.toString(), map, rm));
	}

	@Override
	public List<Desk> list(Desk desk, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		RowMapper<Desk> rm = new BeanPropertyRowMapper<Desk>(Desk.class);
    StringBuffer sql = new StringBuffer("select d.id,d.organization_id,d.name");
    sql.append(",d.increase_position,d.state,d.poker1,d.poker2,d.poker3");
		sql.append(",d.poker4,d.poker5,d.remark,d.order_num");
		sql.append(",d.blind,d.next_position,d.change_jeton");
		sql.append(",d.create_time,d.create_user,d.update_time,d.update_user");
    sql.append(",sogzn.organization_name organizationName,suser.username createName ");
    sql.append(" from th_desk d ");
    sql.append(" inner join sys_user suser on suser.user_id=d.create_user ");
    sql.append(" inner join sys_organization sogzn on sogzn.organization_id=d.organization_id and sogzn.deleted=0 ");
    sql.append(" where d.deleted=0 ");
		if(loginUser.getUserId() != UserConstant.ADMIN_ID){
      sql.append(" and d.organization_id=:tableOrganizationId ");
      map.put("tableOrganizationId", loginUser.getOrganizationId() + "");
    }
    if(Utils.isNotEmpty(desk)){
//			if(Utils.isNotEmpty(desk.getName())){
//				sql.append(" and instr(d.name,:name)>0 ");
//				map.put("name", desk.getName());
//			}
      if(Utils.isNotEmpty(desk.getSort())&&Utils.isNotEmpty(desk.getOrder())){
				sql.append(" order by d."+desk.getSort()+" "+desk.getOrder());
			} else {
				sql.append(" order by d.create_time desc");
			}
		}
		getMapParameter(sql, map);
		return jdbcTemplate.query(sql.toString(),map, rm);
	}

	@Override
	public Desk getById(Desk desk) {
		RowMapper<Desk> rm = new BeanPropertyRowMapper<Desk>(Desk.class);
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql = new StringBuffer("select d.id,d.organization_id,d.name");
    sql.append(",d.increase_position,d.state,d.poker1,d.poker2,d.poker3");
		sql.append(",d.poker4,d.poker5,d.remark,d.order_num");
		sql.append(",d.blind,d.next_position,d.change_jeton");
		sql.append(",d.create_time,d.create_user,d.update_time,d.update_user");
    sql.append(",sogzn.organization_name organizationName,suser.username createName ");
    sql.append(" from th_desk d ");
    sql.append(" inner join sys_user suser on suser.user_id=d.create_user ");
    sql.append(" inner join sys_organization sogzn on sogzn.organization_id=d.organization_id and sogzn.deleted=0 ");
    sql.append(" where d.deleted=0 and d.id=:id ");
		map.put("id", desk.getId());
		getMapParameter(sql, map);
		try {
			return jdbcTemplate.queryForObject(sql.toString(), map, rm);
		} catch (EmptyResultDataAccessException e) {
			return null;
		}
	}

	@Override
	public Integer save(Desk desk, User loginUser) {
		GeneratedKeyHolder generatedKeyHolder = new GeneratedKeyHolder();
		KeyHolder keyHolder = new GeneratedKeyHolder();
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("insert into th_desk (");
    sql.append("organization_id,name,increase_position,poker1");
		sql.append(",poker2,poker3,poker4,poker5");
		sql.append(",blind,next_position");
		sql.append(",remark,order_num,create_user,create_time) values ( ");
    sql.append(":organization_id,:name,:increase_position,:poker1");
		sql.append(",:poker2,:poker3,:poker4,:poker5");
		sql.append(",:blind,:next_position");
		sql.append(",:remark,:order_num,:create_user,now())");
    map.put("organization_id", loginUser.getOrganizationId() + "");
    map.put("name", desk.getName());
    map.put("increase_position", desk.getIncreasePosition()+"");
    map.put("poker1", desk.getPoker1());
    map.put("poker2", desk.getPoker2());
    map.put("poker3", desk.getPoker3());
    map.put("poker4", desk.getPoker4());
    map.put("poker5", desk.getPoker5());
    map.put("remark", desk.getRemark());
    map.put("blind", desk.getBlind() + "");
    map.put("next_position", desk.getNextPosition() + "");
    map.put("order_num", desk.getOrderNum()==null?null:desk.getOrderNum()+"");
		map.put("create_user", loginUser.getUserId() + "");
		getMapParameter(sql, map);
		//		System.err.println("受到影响的行数=" + retVal + ",
		int retVal = this.jdbcTemplate.update(sql.toString(), new SqlParameterSource() {
			@Override
			public boolean hasValue(String paramName) {
				return map.containsKey(paramName);
			}

			@Override
			public Object getValue(String paramName) throws IllegalArgumentException {
				return map.get(paramName);
			}
		}, generatedKeyHolder);

		Integer id = generatedKeyHolder.getKey().intValue();
		return id;
	}

	@Override
	public boolean update(Desk desk, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("update th_desk set ");
    sql.append("name=:name,state=:state,poker1=:poker1 ");
		sql.append(",poker2=:poker2,poker3=:poker3,poker4=:poker4 ");
		sql.append(",blind=:blind,next_position=:next_position,change_jeton=:change_jeton");
		sql.append(",poker5=:poker5,remark=:remark,order_num=:order_num ");
		sql.append(",update_time=now(),update_user=:update_user where id=:id ");
    map.put("name", desk.getName());
    map.put("state", desk.getState());
    map.put("poker1", desk.getPoker1());
    map.put("poker2", desk.getPoker2());
    map.put("poker3", desk.getPoker3());
    map.put("poker4", desk.getPoker4());
    map.put("poker5", desk.getPoker5());
    map.put("remark", desk.getRemark());
		map.put("blind", desk.getBlind() + "");
		map.put("next_position", desk.getNextPosition() + "");
		map.put("change_jeton", desk.getChangeJeton() + "");
    map.put("order_num", desk.getOrderNum()==null?null:desk.getOrderNum()+"");
		map.put("update_user", loginUser.getUserId() + "");
		map.put("id", desk.getId());
		getMapParameter(sql, map);
		int count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}

	@Override
	public boolean remove(Desk desk, User loginUser) {
		int count = 0;
		Map<String, String> map = null;
		if(desk.getId().length()>0){
			for(String ID : desk.getId().split(",")){
				map  = new HashMap<String, String>();
				if(ID==null || ID.equals("")){ break; }
				StringBuffer sql =new StringBuffer("update th_desk set deleted=1" +
						",update_user=:update_user, update_time=now() "
						+ " where id =:id ");
				map.put("update_user", loginUser.getUserId() + "");
				map.put("id", ID);
				getMapParameter(sql, map);
				count = jdbcTemplate.update(sql.toString(),map);
			}
		}
		return count > 0;
	}
}
